package com.yitongmao.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yitongmao.bean.GoodsListBean1;
import com.yitongmao.yitmfruit.R;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;


public class ShoppingCartAdapter extends BaseAdapter {
    private boolean flag = true;//是否显示true - 编辑 / false - 完成
    private Context mContext;
    private List<GoodsListBean1> shoppingCartBeanList;
    private CheckInterface checkInterface;
    private ModifyCountInterface modifyCountInterface;

    public ShoppingCartAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return shoppingCartBeanList == null ? 0 : shoppingCartBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return shoppingCartBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final Viewholder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_shopping_cart_layout1, viewGroup, false);
            holder = new Viewholder(convertView);
            convertView.setTag(holder);
            //适配
            AutoUtils.autoSize(convertView);
        } else {
            holder = (Viewholder) convertView.getTag();
        }
        final GoodsListBean1 goodsListBean = shoppingCartBeanList.get(position);
        /**
         * 把数据添加到控件上
         */
        //是否选中
        if (flag) {//编辑
            final boolean choosed = goodsListBean.isCheck();
            if (choosed) holder.ckChoose.setChecked(true);
            else holder.ckChoose.setChecked(false);
        } else {//完成
            final boolean isDelCheck = goodsListBean.isDelCheck();
            if (isDelCheck) holder.ckChoose.setChecked(true);
            else holder.ckChoose.setChecked(false);
        }
        //图片资源
        Glide.with(mContext)
                .load(goodsListBean.getThumb())
                .fitCenter()
                .into(holder.ivShowPic);
        //商品名称
        holder.tvCommodityName.setText(goodsListBean.getTitle());

        //商品价格
        holder.tvCommodityPrice.setText("￥" + (goodsListBean.getTradePrice() / 1.00) + "/吨");

        //商品数量
        holder.tvCommodityShowNum.setText(goodsListBean.getNum() + "");

        //单选框按钮
        holder.ckChoose.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (flag)
                            goodsListBean.setCheck(((CheckBox) v).isChecked());
                        else goodsListBean.setDelCheck(((CheckBox) v).isChecked());
                        checkInterface.checkGroup(position, ((CheckBox) v).isChecked());//向外暴露接口
                    }
                }
        );
        //增加按钮
        holder.ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                modifyCountInterface.doIncrease(position, holder.tvCommodityShowNum, holder.ckChoose.isChecked());//暴露增加接口
            }
        });
        //选择规格
        holder.tvCommoditySpecifications.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                modifyCountInterface.doUpdateSpecifications(position);//暴漏修改接口
            }
        });
        //删减按钮
        holder.ivSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                modifyCountInterface.doDecrease(position, holder.tvCommodityShowNum, holder.ckChoose.isChecked());//暴露删减接口
            }
        });

        return convertView;
    }

    /**
     * 是否显示可编辑
     *
     * @param flag
     */
    public void isShow(boolean flag) {
        this.flag = flag;
        notifyDataSetChanged();
    }

    /**
     * ViewHolder复用类
     */
    private class Viewholder {

        CheckBox ckChoose;
        ImageView ivShowPic, ivSub, ivAdd;
        TextView tvCommodityName, tvCommoditySpecifications, tvCommodityPrice, tvCommodityShowNum;

        public Viewholder(View itemView) {
//            是否选中
            ckChoose = (CheckBox) itemView.findViewById(R.id.ck_chose);
//            水果图片
            ivShowPic = (ImageView) itemView.findViewById(R.id.iv_show_pic);
//            减少一个
            ivSub = (ImageView) itemView.findViewById(R.id.iv_sub);
//            添加一个
            ivAdd = (ImageView) itemView.findViewById(R.id.iv_add);
//            商品名称
            tvCommodityName = (TextView) itemView.findViewById(R.id.tv_commodity_name);
//            选择规格
            tvCommoditySpecifications = (TextView) itemView.findViewById(R.id.tv_commodity_specifications);
//            商品价格加个
            tvCommodityPrice = (TextView) itemView.findViewById(R.id.tv_commodity_price);
//            商品数量
            tvCommodityShowNum = (TextView) itemView.findViewById(R.id.tv_commodity_show_num);
        }
    }

    /**
     * 单选接口
     *
     * @param checkInterface
     */
    public void setCheckInterface(CheckInterface checkInterface) {
        this.checkInterface = checkInterface;
    }

    /**
     * 改变商品数量接口
     *
     * @param modifyCountInterface
     */
    public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {
        this.modifyCountInterface = modifyCountInterface;
    }

    /**
     * 设置商品集合数据
     *
     * @param shoppingCartBeanList
     */
    public void setShoppingCartBeanList(List<GoodsListBean1> shoppingCartBeanList) {
        this.shoppingCartBeanList = shoppingCartBeanList;
        notifyDataSetChanged();
    }

    /**
     * 复选框接口
     */
    public interface CheckInterface {
        /**
         * 组选框状态改变触发的事件
         *
         * @param position  元素位置
         * @param isChecked 元素选中与否
         */
        void checkGroup(int position, boolean isChecked);
    }


    /**
     * 改变数量的接口
     */
    public interface ModifyCountInterface {
        /**
         * 增加操作
         *
         * @param position      元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param isChecked     子元素选中与否
         */
        void doIncrease(int position, View showCountView, boolean isChecked);

        /**
         * 删减操作
         *
         * @param position      元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param isChecked     子元素选中与否
         */
        void doDecrease(int position, View showCountView, boolean isChecked);

        /**
         * 删除子item
         *
         * @param position
         */
        void childDelete(int position);

        /**
         * 删除子item
         *
         * @param position
         */
        void doUpdateSpecifications(int position);

    }


}
